package coml.jiyun.hp.day09;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class MyViewGroup extends LinearLayout {

    String TAG = "MyViewGroup";
    private float mDownX;
    private float mDownY;

    public MyViewGroup(Context context) {
        super(context);
    }

    //分发事件的方法
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d(TAG, "dispatchTouchEvent: "+ev.getAction());
        return super.dispatchTouchEvent(ev);
    }

    //拦截事件的方法
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.d(TAG, "onInterceptTouchEvent: "+ev.getAction());
        //super.onInterceptTouchEvent(ev) 事件分发
        //true: 事件拦截,事件给到自己的onTouchEvent()
        //false:和返回super.一样

        ///父容器想要垂直的事件,子view想要水平的滑动事件
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                mDownX = ev.getX();
                mDownY = ev.getY();
                break;

             case MotionEvent.ACTION_HOVER_MOVE:
                 float moveX = ev.getX();
                 float moveY = ev.getY();
                 float dx = moveX - mDownX;
                 float dy = moveY - mDownY;
                 if (Math.abs(dx) < Math.abs(dy)){
                     //垂直
                     return true;
             }
        }
        return super.onInterceptTouchEvent(ev);
    }

    //处理事件的方法
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(TAG, "onTouchEvent: "+event.getAction());
        return true;
    }
}
